﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// StudentsListEditor.xaml 的交互逻辑
    /// </summary>
    public partial class StudentsListEditor : MetroWindow
    {
        public StudentsListEditor(string filePath)
        {
            InitializeComponent();

            this.filePath = filePath;
            this.Loaded += StudentsListEditor_Loaded;
        }

        private void StudentsListEditor_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (File.Exists(this.filePath))
                {
                    tbx.Text = File.ReadAllText(this.filePath);
                }

                if (string.IsNullOrEmpty(tbx.Text))
                {
                    tbx.Text = Properties.Resources.SampleClassFileContent;
                }
            }
            catch (Exception ex)
            {
                LMessageBox.Show(ex.Message);
            }
        }

        private string filePath;

        public string FilePath
        {
            get
            {
                return filePath;
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
            return;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            #region 保存编辑的班级列表
            try
            {
                File.WriteAllText(this.filePath, tbx.Text, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                LMessageBox.Show(ex.Message);
            }
            #endregion 保存编辑的班级列表

            this.DialogResult = true;
            this.Close();
            return;
        }
    }
}
